package Application.Layer;

import java.util.ArrayList;
import Application.AppJoint;
import Application.Color.BlendLayer;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import Application.AppJoint;

/**
 * 图层代理
 */
public class LayerDelegate {
    public ArrayList<Layer> getLayers() {
        return layers;
    }

    public void setLayers(ArrayList<Layer> layers) {
        this.layers = layers;
    }

    private ArrayList<Layer> layers = new ArrayList<>();
    private static LayerDelegate ins = new LayerDelegate();

    private LayerDelegate() { }

    public static LayerDelegate getInstance() {
        return ins;
    }

    // 新建图层，返回新图层id
    public int createNewLayer(String name, GridPane root, WritableImage ... writableImages) {
        // 获取宽度
        int width = AppJoint.getInstance().getCanvasWidth();
        // 获取高度
        int height = AppJoint.getInstance().getCanvasHeight();
        // 创建新图层

        Layer layer;

        if(writableImages.length==0){
            layer = new Layer(name,width, height);
        }else{
            layer = new Layer(name,width,height,writableImages[0]);
        }

        layer.setRoot(root);

        layer.getRoot().setBackground(new Background(new BackgroundFill(AppJoint.unselectedColor, null, null)));

        // 加入列表
        layers.add(0,layer);
        //System.out.println("aaa");

        this.refreshAll();

        AppJoint.getInstance().flush();

        return layer.getId();
    }

    // 删除编号为id的图层，返回图层在数组中的位置
    public int deleteLayer(int id) {
        int i = 0;
        for(i = 0; i < layers.size(); i++)
        {
            if(layers.get(i).getId() == id) {
                break;
            }
        }
        layers.remove(i);

        this.refreshAll();

        AppJoint.getInstance().flush();
        return i;
    }

    // 上移一层, 返回索引
    public int moveUp(int id) {
        int i = 0;
        for(i = 0; i < layers.size(); i++) {
            if(layers.get(i).getId() == id) {
                break;
            }
        }

        if(i == 0) {
            return 0;
        }

        Layer temp = layers.get(i);
        layers.set(i, layers.get(i - 1));
        layers.set(i - 1, temp);

        return i;
    }

    // 下移一层，返回索引
    public int moveDown(int id) {
        int i = 0;
        for(i = 0; i < layers.size(); i++) {
            if(layers.get(i).getId() == id) {
                break;
            }
        }

        if(i == layers.size() - 1) {
            return i;
        }

        Layer temp = layers.get(i);
        layers.set(i, layers.get(i + 1));
        layers.set(i + 1, temp);

        return i;
    }

    public void combineLayer(){

    }

    // 设置图层可见性
    public void setVisible(int index, boolean visible) {
        layers.get(index - 1).setVisible(visible);
    }

    // 设置图层锁定
    public void setLocked(int index, boolean locked) {
        layers.get(index - 1).setLocked(locked);
    }

    // 设置图层透明度
    public void setOpacity(int index, int opacity) {
        layers.get(index - 1).setAlpha(opacity);
    }

    // 通过id寻找数组中的索引
    public int getIndexGivenId(int id) {
        for(int i = 0; i < layers.size(); i++) {
            if(layers.get(i).getId() == id) {
                return i;
            }
        }
        return -1;
    }

    public int getIdGivenIndex(int index){
        return layers.get(index).getId();
    }

    public void refreshSingle(int x, int y) {
//        Color ans = layers.get(layers.size() - 1).getColor(x, y);
//        for(int i = layers.size() - 2; i >= 0; i--) {
//            int blendMode = layers.get(i).getBlendMode();
//            Color c1 = layers.get(i).getColor(x, y);
//
//            ans = AppJoint.getInstance().blend(c1, ans, blendMode);
//        }
//
//        AppJoint.getInstance().getOutImg().setPixelColor(x, y, ans);
        int ans = 0;
//        if(layers.size()==0){
//            ans = 0;
//        } else {
//            ans = layers.get(layers.size() - 1).getArgb(x, y);
//            for(int i = layers.size() - 2; i >= 0; i--) {
//                int blendMode = layers.get(i).getBlendMode();
//                int c1 = layers.get(i).getArgb(x, y);
//
//                ans = BlendLayer.getInstance().blend(c1, ans, blendMode);
//            }
//        }
        int it = layers.size() - 1;
        while(it >= 0) {
            if(layers.get(it).getVisible()) {
                ans = layers.get(it).getArgb(x, y);
                it--;
                break;
            }
            it--;
        }
        while(it >= 0) {
            if(layers.get(it).getVisible()) {
                int blendMode = layers.get(it).getBlendMode();
                int c1 = layers.get(it).getArgb(x, y);

                ans = BlendLayer.getInstance().blend(c1, ans, blendMode);
            }
            it--;
        }

        AppJoint.getInstance().getOutImg().setPixelArgb(x, y, ans);
    }



    public void refreshAll() {
        int width = AppJoint.getInstance().getCanvasWidth();
        int height = AppJoint.getInstance().getCanvasHeight();
        for(int i = 0; i < width; i++) {
            for(int j = 0; j < height; j++) {
                refreshSingle(i, j);
            }
        }
    }

    public Layer getLayerById(int id) {
        return layers.get(this.getIndexGivenId(id));
    }


}
